package cn.imnaer.service.rpc.collect.application;

import java.util.ArrayList;
import java.util.List;

import org.bson.BSONObject;
import org.bson.BasicBSONObject;

import cn.imnaer.common.factory.TimeHelper;
import cn.imnaer.service.persistence.model.Collect;
import cn.imnaer.service.persistence.model.Scenic;
import cn.imnaer.service.rest.resquest.model.CollectParams;
import cn.imnaer.service.rest.view.model.CollectGroupView;
import cn.imnaer.service.rest.view.model.RecommendView;
import cn.imnaer.service.rpc.collect.CollectComponentRpcApi;
import cn.imnaer.service.rpc.scenic.dao.ScenicDaoImpl;
import cn.imnaer.service.rpc.scenic.process.ScenicCommentService;

public class CollectComponentRpcApiImpl implements CollectComponentRpcApi {
	private CollectDaoImpl collectDao = CollectDaoImpl.getInstance();
	private ScenicCommentService scenicCommentService = new ScenicCommentService();

	public RecommendView createCollect(CollectParams collectParams) {

		Collect collect = collectParams.build();
		collect.setCreateTime(TimeHelper.getCurrentTime());
		collectDao.insert(collect, true);
		String scenicId = collectParams.getScenicId();
		// 更新景点的收藏数
		ScenicDaoImpl.getInstance().incrementCounterByPk(scenicId, "collectCount");
		RecommendView view = new RecommendView();
		view.setScenicType(RecommendView.SCENIC);
		view.setRecommendId(scenicId);
		view.setIsCollected(true);
		return view;
	}

	/**
	 * 
	 * 得到收藏组合的列表
	 */
	@Override
	public List<CollectGroupView> getCollectGroupList(String userId) {
		BSONObject matcher = new BasicBSONObject();
		matcher.put("userId", userId);
		BSONObject order = new BasicBSONObject();
		order.put("createTime", -1);
		List<Collect> collectList = collectDao.findAll(matcher, order);
		List<CollectGroupView> views = new ArrayList<>();
		for (Collect collect : collectList) {

			String scenicId = collect.getScenicIds().get(0);
			Scenic scenic = ScenicDaoImpl.getInstance().findOneByPk(scenicId);
			CollectGroupView view = CollectGroupView.valueOf(collect);
			view.setUserId(userId);
			view.setImage(scenic.getImages().get(0));
			views.add(view);
		}
		return views;
	}

	/**
	 * 
	 * 得到收藏组合里面的列表
	 * 
	 */
	@Override
	public List<RecommendView> getCollectGroupItemList(String groupId) {
		List<RecommendView> recommendViewList = new ArrayList<>();
		Collect collect = this.collectDao.findOneByPk(groupId);
		if (collect == null) {
			return recommendViewList;
		}
		List<String> scenicIds = collect.getScenicIds();
		List<Scenic> scenicList = ScenicDaoImpl.getInstance().findListByPkArray(scenicIds);
		for (Scenic scenic : scenicList) {
			RecommendView recommendView=	RecommendView.valueOf(scenic);
			recommendView.setAvatar(scenicCommentService.getFirstCommentUserId(scenic.getScenicId()));
			recommendViewList.add(recommendView);
		}
		return recommendViewList;
	}

	/**
	 * 
	 * 
	 * 得到收藏的景点列表.
	 * 
	 * 
	 */
	@Override
	public List<String> getMyCollectScenicList(String userId) {
		List<String> ids = new ArrayList<>();
		BSONObject matcher = new BasicBSONObject();
		matcher.put("userId", userId);
		List<Collect> collects = CollectDaoImpl.getInstance().findAll(matcher, new BasicBSONObject());
		for (Collect collect : collects) {
			ids.addAll(collect.getScenicIds());
		}
		return ids;
	}

	@Override
	public void deleteCollect(CollectParams collectParams) {
		String groupId = collectParams.getCollectGroupId();
		String scenicId = collectParams.getScenicId();
		Collect collect = null;
		BSONObject matcher = new BasicBSONObject();
		matcher.put("groupId", groupId);
		matcher.put("userId", collectParams.getUserId());
		collect = collectDao.findOne(matcher);
		List<String> scenicIds = collect.getScenicIds();
		scenicIds.remove(scenicId);
		collectDao.update(matcher, collect);
		// 更新景点的收藏数
		ScenicDaoImpl.getInstance().decrementCounterByPk(scenicId, "collectCount");

	}

	@Override
	public CollectGroupView createCollectGroup(String userId, String groupName) {
		Collect collect = new Collect();
		collect.setName(groupName);
		collect.setUserId(userId);
		this.collectDao.insert(collect, true);
		return CollectGroupView.valueOf(collect);

	}
}
